package com.kakaxi.mybilibili.adapter.tag;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.kakaxi.mybilibili.R;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 分区直播间标签流式布局适配器
 * Created by kakaxi on 16/11/19.
 */

public class LiveCategoryRoomsTagFlowAdapter extends TagAdapter<String> {

    /** 上下文 **/
    private Context mContext;

    /** 标签点击监听器 **/
    private OnTagClickListener mListener;

    public LiveCategoryRoomsTagFlowAdapter(Context context, List<String> datas) {
        super(datas);
        mContext = context;
    }

    @Override
    public View getView(FlowLayout parent, final int position, String s) {
        final TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout
                .item_tag_live_category_rooms_tag, parent, false);
        tv.setText(s);
        tv.setTag(s);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onTagClick(tv, position);
                }
            }
        });
        return tv;
    }

    /**
     * 设置选中标签
     *
     * @param position 位置
     */
    public void setSelectedTag(int position) {
        Set<Integer> checkedSet = new HashSet<>();
        checkedSet.add(position);
        setSelectedList(checkedSet);
    }

    /**
     * 设置监听器
     *
     * @param tagClickListener 监听器
     */
    public void setOnTagClickListener(OnTagClickListener tagClickListener) {
        mListener = tagClickListener;
    }

    /**
     * 获取监听器
     *
     * @return 监听器
     */
    public OnTagClickListener getOnTagClickListener() {
        return mListener;
    }

    /**
     * 标签点击监听器
     */
    public interface OnTagClickListener {

        /**
         * 点击事件
         *
         * @param view     视图
         * @param position 位置
         */
        void onTagClick(View view, int position);

    }


}
